#!/bin/bash

if [ -z $1 ];then
    echo "error command, support <start> <stop> <restart> <build>"
    exit
fi

#停止容器操作
if [[ "$1" == "build" ]] || [[ "$1" == "stop" ]] || [[ "$1" == "restart" ]];then
	echo '正在清理docker环境...'
	docker stop `docker ps -q`
	docker rm `docker ps -a -q
	echo '清理完成...'`
fi

#替换本地的文件
if [[ "$1" == "start" ]] || [[ "$1" == "build" ]] || [[ "$1" == "restart" ]];then

	#没有本地的ip文件存在
	if [ ! -f "./ip.txt" ];then
		./getip.sh
		sleep 0.5
	fi
	#addr=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'| head -n 1|tr -d "addr:")
	addr=$(cat ./ip.txt)

	echo '匹配到本地的IP为：'$addr

	sed -i "" "s/^xdebug.remote_host.*/xdebug.remote_host = $addr/g" /Volumes/d/docker/lnmp/php73/php.ini
	echo '修改完成'
	sleep 0.5
fi

if [[ "$1" == "start" ]] || [[ "$1" == "restart" ]];then
	
	echo '正在启动docker...'

	docker-compose up -d  
fi


if [[ "$1" == "build" ]];then
	
	echo '重新建立环境。正在启动docker...'

	docker-compose up -d --build  
fi